<?php
	include '../include/functions.php';
	//Connect To Database
	dbConnect();
?>
<html>
  <HEAD>
    <TITLE>JoshHughes.com NFL Football Pool Site</TITLE>
    <link rel="stylesheet" href="css/NFLPool.css" type="text/css">
</head>
<body leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<?
/*
	// Zero Out All Scores, Ranks, PrevRanks
	$SQL = "UPDATE NFLUsers SET PlayOffs=0";
	mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br>$SQL<br>");
	// Save off all the stats?		
		
	//Get RandomMonkey's Rank
	$result = mysql_query("SELECT Rank FROM NFLUsers Where ID=7") or die("Cannot query the database.<br>" . mysql_error());
	if($myrow = mysql_fetch_array($result))
		$MonkeyRank = $myrow["Rank"];
	echo "MonkeyRank =$MonkeyRank <br/>";
	// Set Playoffs = 1 For ther top 16 users, Take 17th user instead of RandomMonkey if Monkey is ranked 16 or less
	if ($MonkeyRank <= 16)
		$SQL = "UPDATE NFLUsers SET PlayOffs = 1 WHERE Rank <= 17 AND ID <> 7";
	else
		$SQL = "UPDATE NFLUsers SET PlayOffs = 1 WHERE Rank <= 16";
	echo "$SQL<br/>";
	mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br>$SQL<br>");
	//----------------------------------------------------
	
	
	// Select all the users who made the playoffs
	$SQL = "SELECT ID, username, email, Score, Rank FROM NFLUsers WHERE PlayOffs = 1 ORDER BY username";
	//Open Record Set using SQL Query
	$Emailresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br>$SQL<br>");
	//----------------------------------------------------
	
	// Zero Out All Scores, Ranks, PrevRanks
	$SQL = "UPDATE NFLUsers SET Score=0,WeightedScore=0,Rank=1,PrevRank=0,PicksMade=0,Survivor='OUT'";
	mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error() . "<br>$SQL<br>");
	//----------------------------------------------------
	
	
	//set up some email varibables
	$sentTo = "";
	$MailFrom = "From: Football@joshhughes.com";
	$MailSubject = "Football Pool Playoffs";
	do{
		if ($Emailrow["email"] != "") 
		{   //add user's email to the list.
			$MailTo = $Emailrow["username"]." <".$Emailrow["email"].">";
			$sentTo .= $MailTo.", ";
			// Compose Mail Body
			$MailBody = "Congratulations ".$Emailrow["username"]."!\n\nYou are one of only 16 users who qualified for the Football Pool Playoffs.  With a score of ". $Emailrow["Score"].", you were ranked ";
			// In case the Monkey was in the top 16 qand we took the 17th user.
			if ($Emailrow["Rank"] == 17)
				$MailBody.= ordinal_suffix(16).".\n";
			else
				$MailBody.= ordinal_suffix($Emailrow["Rank"]).".\n";
			
			$MailBody .= "\nIn the playoffs you'll make picks the exact same way you did throughout the regular season.  Everyone's scores & ranks will be reset to zero in a few days.  The straight score & weighted score standings will display scores acheived in the playoff weeks.  There is a bracket drawn out at http://football.joshhughes.com/playoffs.php that will show the head-to-head matchup you'll be in next week.  There are only four games so make sure you make a \"Magic Number\" guess in case you get the same number of correct picks as your opponent.  Even if you do not win your head-to-head matchup you will still be allowed to continue making picks all the way through the Super Bowl (for a total of 11 games).  So there cold be two winners, most correct picks and head-to-head champion.  (Any tie in most correct picks out of 11 will be decided by the \"Magic Number\" from the Super Bowl.)\n\nLet me know if you have any questions.\n\n-Josh\nFootball Pool Administrator";
			
			mail($MailTo, $MailSubject, $MailBody, $MailFrom);  //message send
			echo "<H1>MailTo: $MailTo</H1>";
		}
	}while($Emailrow = mysql_fetch_array($Emailresult));

	/*
	$MailTo = "<Football@joshhughes.com>";
	$MailFrom = "From: Football@joshhughes.com";
	$MailSubject = "PLayoff Email Script has been run";
	$MailBody = "The playoffSetup.php script was run on ".date('l F jS, Y')." at ".date('g:i A',time() + (3600 * 3)).".<br>$SentTo";
	//Send an Email to me notifying me of a request
	mail($MailTo, $MailSubject, $MailBody, $MailFrom);  //message send
	echo "<H1>$MailBody</H1>";
	
	// Setup Week 18 Games
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (18, '2008-1-5', 1,'Redskins','Seahawks','16:30:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (18, '2008-1-5', 2,'Jaguars','Steelers','20:00:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (18, '2008-1-6', 3,'Giants','Buccaneers','13:00:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (18, '2008-1-6', 4,'Titans','Chargers','16:30:00')") or die (mysql_error());
	
	// Setup Week 19 Games
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (19, '2008-1-12', 1,'','Cowboys','16:30:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (19, '2008-1-12', 2,'','Patriots','20:00:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (19, '2008-1-13', 3,'','Packers','13:00:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (19, '2008-1-13', 4,'','Colts','16:30:00')") or die (mysql_error());
	// Setup Week 20 Games
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (20, '2008-1-20', 1,'','','13:00:00')") or die (mysql_error());
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (20, '2008-1-20', 2,'','','16:30:00')") or die (mysql_error());
	// Setup Week 21 Game
	mysql_query ("INSERT INTO `NFLSchedule` (Week,GameDate,GameNum,Away,Home, GameTime) VALUES (21, '2008-2-3', 1,'','','18:20:00')") or die (mysql_error());
	*/
?>
</body>
</html>